package cn.com.scitc.test01.login;


import cn.com.scitc.test01.dao.MyUserDao;
import cn.com.scitc.test01.model.Myuser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;


@Controller
public class pdlogin {
    @Autowired
    private MyUserDao dao;

    @RequestMapping("/pd")
    public void pd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        获取输入的账号和密码
        Myuser user = new Myuser();
        user.setLogid(req.getParameter("loginid"));
        user.setPwd(req.getParameter("pwd"));

        Iterable<Myuser> all = dao.findAll();

        for (Myuser u : all) {
//            判断账号是否正确
            if (u.getLogid().equals(user.getLogid()) && u.getPwd().equals(user.getPwd())) {
                Optional<Myuser> byId = dao.findById(u.getId());
                user = byId.get();
                req.getSession().setAttribute("user", user);
                resp.sendRedirect("/loguser");
                return;
            }
        }

        req.getSession().setAttribute("msg", "账号或密码错误");
        resp.sendRedirect("/login");
    }

}
